package com.aimuceliang.aimuceliang.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.aimuceliang.aimuceliang.Constants;
import com.aimuceliang.aimuceliang.bean.LongChengBean;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.green_dao.LongCheng;
import com.aimuceliang.aimuceliang.green_dao.LongChengDaoUtils;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.ui.adapter.CommonAdapter;
import com.aimuceliang.aimuceliang.ui.adapter.CommonViewHolder;
import com.aimuceliang.aimuceliang.utils.BluetoothChat;
import com.aimuceliang.aimuceliang.utils.DataUtils;
import com.aimuceliang.aimuceliang.utils.DateUtils;
import com.aimuceliang.aimuceliang.utils.FtpManager;
import com.aimuceliang.aimuceliang.utils.JXLUtils;
import com.aimuceliang.aimuceliang.utils.XLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.OnClick;


public class LongChengActivity extends BaseActivity {


    @BindView(R.id.et_earid)
    EditText etEarid;
    @BindView(R.id.et_weight)
    EditText etWeight;
    @BindView(R.id.listView)
    ListView listView;

    private LongChengDaoUtils daoUtils;
    private MyAdapter adapter;
    private ArrayList<LongCheng> longChengs;


    @Override
    protected int initContentView() {
        return R.layout.activity_longcheng;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("笼秤测量");
        daoUtils = new LongChengDaoUtils(this);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        BluetoothChat.getBluetoothChat(0).shutdownClient();
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        longChengs = new ArrayList<>();
        longChengs.addAll(daoUtils.queryAllLongCheng());
        while(longChengs.size() > 3) {
            longChengs.remove(0);
        }
        Collections.reverse(longChengs);
        adapter.bindData(longChengs);
    }

    @OnClick({R.id.lc_next_btn, R.id.lc_data_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lc_next_btn:
                String earid = etEarid.getText().toString().trim();
                String weight = etWeight.getText().toString().trim();

                if (earid.isEmpty()||weight.isEmpty()) {
                    Toast.makeText(this, "数据为空", Toast.LENGTH_SHORT).show();
                    //return;
                } else {
                    LongCheng longCheng = new LongCheng();
                    longCheng.setEarid(earid);
                    longCheng.setWeight(weight);
                    longCheng.setId(null);
                    longCheng.setDeviceid(Constants.DEVICE_ID);
                    longCheng.setTime(DateUtils.getCurrentFormatTime());
                    boolean isInsertSuccess = daoUtils.insertLongCheng(longCheng);
                    if (isInsertSuccess) {
                        etEarid.setText("");
                        etWeight.setText("");
                        longChengs.add(longCheng);

                        while(longChengs.size() > 3) {
                            longChengs.remove(0);
                        }
                        Collections.reverse(longChengs);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.lc_data_btn:
                startActivityForResult(new Intent(LongChengActivity.this, LCDataActivity.class), 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(1 == resultCode) {
            longChengs.clear();
            adapter.notifyDataSetChanged();
        }
    }

    // Called in Android UI's main thread
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(LongChengBean event) {
        LongCheng longCheng = new LongCheng();

        longCheng.setEarid(event.strEarNum);
        longCheng.setWeight(event.strWeight);

        //etEarid.setText(event.strEarNum);
        etWeight.setText(event.strWeight);
    }

    private class MyAdapter extends CommonAdapter<LongCheng> {

        MyAdapter() {
            super(R.layout.item_longcheng_measure, LongChengActivity.this);
        }

        @Override
        public void convert(CommonViewHolder holder, LongCheng longCheng) {
            //初始化点击事件,防止复用导致点击出错
            holder.getView(R.id.tv_delete).setOnClickListener(null);
            holder.setText(R.id.tv_ear_id, longCheng.getEarid());
            holder.setText(R.id.tv_weight, longCheng.getWeight());

            holder.getView(R.id.tv_delete).setOnClickListener(v -> {
                daoUtils.deleteLongCheng(longCheng);
                longChengs.clear();
                longChengs.addAll(daoUtils.queryAllLongCheng());
                while(longChengs.size() > 3) {
                    longChengs.remove(0);
                }
                Collections.reverse(longChengs);
                adapter.notifyDataSetChanged();
            });
        }
    }
}
